package javase.Thread;

public class ThreadEx {
    public static void main(String[] args) {
        MyNum num = new MyNum();
        num.i = 1;
        Thread t1 = new Thread(new MyThread1(num));
        Thread t2 = new Thread(new MyThread2(num));
        t1.setName("t1");
        t2.setName("t2");
        t1.start();
        t2.start();
    }
}
class MyNum{
    int i;
}

class MyThread1 implements Runnable{
    private MyNum num;

    public MyThread1(MyNum num) {
        this.num = num;
    }

    @Override
    public void run() {
        while (true){
            synchronized (num){
                if(num.i % 2 == 0){
                    try {
                        num.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName() + "---->" + num.i);
                num.i++;
                num.notifyAll();
            }

        }


    }
}

class MyThread2 implements Runnable{
    private MyNum num;

    public MyThread2(MyNum num) {
        this.num = num;
    }

    @Override
    public void run() {
        while(true){
            synchronized (num){
                if(num.i % 2 != 0){
                    try {
                        num.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName() + "---->" + num.i);
                num.i++;
                num.notifyAll();
            }
        }


    }
}